// 物体基类
class substance{
    app_width:number = 0
    app_height:number = 0
    // 初始位置
    protected createpos:any = [0,0]
    // 当前位置
    left:number = 0
    top:number = 0
    // 图片
    img:string=''
    constructor(){
        this.create_position()
    }
    create_position(createpos:any,rectangle:any){
        this.left = this.createpos[0]
        this.top = this.createpos[1]
    }
    // 碰撞检测
    check_impact(){
        
    }
    // 更新移动
    update_position(x:any,y:any){
        let bool = true
        this.get_app_size()
        if (this.top<-100 || this.top>this.app_height+200 || this.left<-100 || this.left>this.app_width+100){
            bool = false
        }
        return bool
    }
    ressize_event(dom:any){
        this.app_height = dom.value.clientHeight
        this.app_width = dom.value.clientWidth
    }
    get_app_size(){
        var win = document.getElementsByTagName('body')
        this.app_width = win[0]?.offsetWidth
        this.app_height = win[0]?.offsetHeight
        return [this.app_width,this.app_height]
    }
}

// 运动体基类
class SprtsBody extends substance{
    // 形状,圆形circular,矩形rectangle
    shape:string='rectangle'
    // 圆形半径
    circularRadius:number = 10
    // 矩形尺寸,宽高
    rectangle = [1,1]
    // 速度
    speed:number=1
    // 运动轨迹,line直线
    trajectory:string = 'line'
    // 运动角度   直线
    lineAngle:number = 0
    create_position(createpos:any,rectangle:any){
        if (createpos ==undefined){
            return super.create_position()
        }
        
        let left = createpos[0]
        let top = createpos[1]
        this.get_app_size()
        if (typeof(left)=='string' && left.endsWith('%')){
            left = parseInt(parseFloat(left)*this.app_width/100)
            top = parseInt(parseFloat(top)*this.app_height/100)
            
        }
        
        let width = this.rectangle[0]
        let height = this.rectangle[1]
        if ('shape' in this && this.shape=='circular'){
            height = width = this.circularRadius*2
        }
        this.left = left - width/2
        this.top = top - height/2-20
    }
    update_position(x:any,y:any){
        const distance = this.speed;
        if (this.trajectory =='line'){
            let newX = this.left
            let newY = this.top
            switch (this.lineAngle){
                case 0:
                    newY -= distance
                    break
                case 90:
                    newX += distance
                    break
                case -90:
                    newX -= distance
                    break
                case 180:
                    newY += distance
                    break
                case -180:
                    newY += distance
                    break
                default:
                    let angleInRadians = (this.lineAngle * Math.PI) / 180;
                    newX = this.left + distance * Math.sin(angleInRadians);
                    newY = this.top - distance * Math.cos(angleInRadians);
            }
            this.top = newY
            this.left = newX
        }
        let bool = true
        this.get_app_size()
        if (this.top<-100 || this.top>this.app_height+200 || this.left<-100 || this.left>this.app_width+100){
            bool = false
        }
        return bool
    }
}

// 生命体基类
class organism extends SprtsBody{
    // 生命值
    hp:number = 1
    // 状态0初始化，1生存，2死亡,3无敌
    status:number = 1
    // 倾斜角度
    lean:number = 0
}

export {substance,organism,SprtsBody}